// 用户数据模块
const TOKEN = uni.getStorageSync("token") || "";
const USERCODE = uni.getStorageSync("userCode") || "";
const USERNAME = uni.getStorageSync("userName") || "";
const USERPHOTO = uni.getStorageSync("userPhoto") || "";

export const state = {
	// 前端token
	token: TOKEN,
	//用户账号
	userName: USERNAME,
	userCode: USERCODE,
	userPhoto:USERPHOTO
}

export const actions = {
	// 登录过期 重新登录
	reLogin({commit}, info) {
		commit("SET_TOKEN", "");
		uni.navigateTo({
			url: '/pages/me/sub/login',
			fail: (e) => {
				console.log(e);
			}
		})
	},
	// 用户是否登录
	hasLogin({commit}, info){
		// console.log(state.token);
		if (state.token) {
			return true;
		} 
		else {			
			return false
		}
	}
}

export const mutations = {
	SET_TOKEN(state, token) {
		state.token = token;
		uni.setStorageSync("token", token);
	},
	SET_USERCODE(state, userCode) {
		state.userCode = userCode;
		uni.setStorageSync("userCode", userCode);
	},
	SET_USERNAME(state, name) {
		state.userName = name;
		uni.setStorageSync("userName", name);
	},
	SET_USERPHOTO(state, photo) {
		state.userPhoto = photo;
		uni.setStorageSync("userPhoto", photo);
	},
	SET_LOGOUT(state) { // 退出登录
		state.token = "";
		uni.setStorageSync("token", "");
	},
	

}

export const getters = {
	// 用户是否登录
	hasLogin: state => {
		if (state.token) {
			return true;
		} else {
			return false
		}
	}
}
